library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;


library lib_ExpoRNS;

entity exponentiation_unit is
	
	generic (
		Z : positive range 1 to 256;
		N : positive ;	-- Vecteur de base utilisé
		R : positive ;       -- Plus proche puissance de 2 de N par valeur supérieure
		log2R : positive;
		k : positive ;	-- Constante nécessaire au calcul
		montgomery : boolean ;
		reel : boolean
	) ;
	port (
		reset : in std_logic;
		clk : in std_logic;
		bit_utile : in std_logic_vector(Z-1 downto 0);
		a     : in std_logic_vector(Z-1 downto 0);
		p     : in std_logic_vector(Z-1 downto 0);  --Puissance
		start : in std_logic;
	
		S : out std_logic_vector(Z-1 downto 0)
	) ;

end entity;

architecture expo_unit1 of exponentiation_unit is

-- Connexion entre la Fsm et la partie de calul de l'exponentiation
component exponentiation_Calc
	generic (
		Z : positive range 1 to 256 := Z;
		montgomery : boolean := montgomery;
		reel : boolean := reel;
		N : positive := N ;
		R : positive := R;
		log2R : positive := log2R;
		k : positive := k
	) ;
	port (
		clk : in std_logic;     
		a       : in  std_logic_vector(Z-1 downto 0);  
		cmdMult : in  std_logic_vector(1 downto 0);
		cmdX    : in  std_logic;
		cmdSave : in std_logic ;
		reset :   in std_logic;
                start : in std_logic;
                is_ok : in std_logic;
		S       : out std_logic_vector(Z-1 downto 0)
	);
end component;


component exponentiation_Fsm
	generic ( Z : positive range 1 to 256 := Z );
	
	port (
		clk : in std_logic ;
		start : in std_logic;
		reset : in std_logic;
		bit_utile : in std_logic_vector (Z-1 downto 0); 
		p : in std_logic_vector(Z-1 downto 0);
		
		a : in  std_logic_vector(Z-1 downto 0);  
		cmdMult : out std_logic_vector(1 downto 0); 
		cmdX : out std_logic;               

                is_ok : out std_logic;             
		cmdSave :  out std_logic            
	);
  
end component;

	
	signal cmdMult :  std_logic_vector(1 downto 0);
	signal  cmdX, cmdSave, is_ok :  std_logic;

begin
	
	--Mapage
	
	FSM: exponentiation_Fsm port map(clk, start, reset, bit_utile,p,a,cmdMult, cmdX, is_ok, cmdSave);
	
	Combinatoire:exponentiation_Calc port map(clk,a, cmdMult, cmdX, cmdSave, reset,start,is_ok, S);
	
end expo_unit1;
